/*!
 * FileName: luogu-1119.cpp
 * This Problem is on luogu. The ID of the problem is 1119. 
 * Copyright(c) 2019 Shu_Yu_Mo
 * MIT Licensed
 * Luogu: https://www.luogu.org/space/show?uid=44615
 * Github: https://github.com/oldsuold/
 * Gitee: https://gitee.com/Shu_Yu_Mo/
 * These words were created by an amazing tool written by Shu_Yu_Mo.
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int _N = 210;
const int _M = _N * _N;
inline int read()
{
    char c = getchar(); int sign = 1; int x = 0;
    while(c > '9' || c < '0') { if(c=='-')sign = -1; c = getchar(); } 
    while(c <= '9' && c >= '0') { x *= 10; x += c - '0'; c = getchar(); }
    return x * sign;
}
int n, m; 
int T[_N];
struct edge{
	int node;
	int w;
	edge(int _node, int _w):node(_node),w(_w){}
};
bool haveBuilt[_N];
int g[_N][_N];
void floyd(int k)
{
	for(register int i = 0;i < n;i++)
		for(register int j = 0;j < n;j++)
			if(g[i][k] + g[k][j] < g[i][j])
				g[i][j] = g[i][k] + g[k][j];

}
int main()
{
	memset(g, 0x3f, sizeof(g));
	memset(T, 0x7f, sizeof(T));
	memset(haveBuilt, false, sizeof(haveBuilt));
	n = read(), m = read();
	for(register int i = 0;i < n;i++) g[i][i] = 0;
	for(register int i = 0;i < n;i++)
		T[i] = read();
	for(register int i = 1;i <= m;i++)
	{
		int tmpx = read();
		int tmpy = read();
		int tmpz = read();
		g[tmpx][tmpy] = g[tmpy][tmpx] = tmpz;
	}
	int Q = read();
	int now = 0;
	for(register int i = 1;i <= Q;i++)
	{
		int x = read(), y = read(), t = read();
		while(T[now] <= t && now < n)
		{
			floyd(now);
			now ++;
		}
		if(T[x] > t || T[y] > t)
			puts("-1");
		else
			if(g[x][y] == 0x3f3f3f3f)
				puts("-1");
			else
				printf("%d\n", g[x][y]);
	}
    return 0;
}
